﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using StockEngine;
using System.Windows.Controls.DataVisualization.Charting;

namespace GUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ReadCSV myReadCSV;
        private PlotGraph myPlotGraph = new PlotGraph();
        private ListStorage myListStorage;
        private Dictionary<string, List<StockNameInfo>> stockMarketDictionary;
        private Dictionary<string, List<StockInfo>> stockInfoDictionary;
        private List<DateSpan> myDateSpan;

        public MainWindow()
        {
            InitializeComponent();
            myListStorage = new ListStorage();
            myReadCSV = new ReadCSV(myListStorage);

            stockMarketDictionary = myListStorage.GetStockMarketDictionary();
            foreach (StockNameInfo stockNameInfo in stockMarketDictionary["NASDAQ"])
            {
                cbStock.Items.Add(stockNameInfo);
            }
            cbStock.SelectedIndex = 0; //Set first stock as default

            myDateSpan = myListStorage.GetDateSpan();
            foreach (DateSpan dateSpan in myDateSpan)
            {
                cbTimeSpan.Items.Add(dateSpan);
            }
            cbTimeSpan.SelectedIndex = 0;
        }

        private void UpdateDb_Click(object sender, RoutedEventArgs e)
        {
            stockInfoDictionary = myReadCSV.ReadCSV_(((StockNameInfo)cbStock.SelectedItem).StockName);
            ((LineSeries)chart.Series[0]).ItemsSource = myPlotGraph.GraphPlotter(
                stockInfoDictionary[((StockNameInfo)cbStock.SelectedItem).StockName], (DateSpan)cbTimeSpan.SelectedItem);

            textBox1.Text = "Database Successfully Updated!";
        }

        private void cbStock_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((LineSeries)chart.Series[0]).ItemsSource = new List<KeyValuePair<DateTime, int>>();
        }

        private void wWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //TODO: Work in progress to have the objects in the GUI change position/size when the GUI changes size.
            /*
            double oHeight = 368, oWidth = 547;
            double Height = gMainWindow.ActualHeight, Width = gMainWindow.ActualWidth;

            if (Height < oHeight)
                gMainWindow.Height = oHeight;

            if (Width < oWidth)
                gMainWindow.Width = oWidth;
             */
        }
    }
}
